package com.warrantyhotel.android.ui;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.db.QueriesCodes;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_Information_Table;
import com.warrantyhotel.android.model.Keys;
import com.warrantyhotel.android.util.JSONParser;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler.AsyncQueryListener;

public class InboxInformationDetail extends Activity implements AsyncQueryListener {

	private TextView txtInboxInformationDescription;
	private TextView txtInboxInformationTitle;
	private NotifyingAsyncQueryHandler mHandler;
	private String info_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inbox_information_detail);

		txtInboxInformationTitle = (TextView) findViewById(R.id.inbox_information_title);
		txtInboxInformationDescription = (TextView) findViewById(R.id.inbox_description_information);
		String guid = getIntent().getExtras().getString(Keys._guid);
		mHandler = new NotifyingAsyncQueryHandler(getContentResolver(), this);
		retrieveNewsRSSbyId(guid);

	}

	private void retrieveNewsRSSbyId(String guid) {
		String[] projectionNews = { Inbox_Information_Table._ID, Inbox_Information_Table.INFO_ID, Inbox_Information_Table.TEXT, Inbox_Information_Table.TITLE };
		mHandler.startQuery(QueriesCodes.INBOX_INFORMATION, Inbox_Information_Table.CONTENT_URI, projectionNews, Inbox_Information_Table.INFO_ID + " = '"
				+ guid + "'", null, null);
	}

	public void doDiscard(View view) {

//		getContentResolver().delete(Inbox_Information_Table.CONTENT_URI, Inbox_Information_Table.INFO_ID + "=?", new String[] { info_id });
		ContentValues values = new ContentValues();
		values.put(Inbox_Information_Table.ISVISIBLE, JSONParser.INBOX_ITEM_INVISIBLE);
		getContentResolver().update(Inbox_Information_Table.CONTENT_URI, values, Inbox_Information_Table.INFO_ID + "=?", new String[] { info_id });
		finish();
	}

	public void doCancel(View view) {
		finish();
	}

	@Override
	public void onQueryComplete(int token, Object cookie, Cursor cursor) {
		switch (token) {
		case QueriesCodes.INBOX_INFORMATION:
			if (cursor != null) {
				startManagingCursor(cursor);
				if (cursor.moveToFirst()) {
					String description = cursor.getString(cursor.getColumnIndex(Inbox_Information_Table.TEXT));
					txtInboxInformationDescription.setText(description);
					info_id = cursor.getString(cursor.getColumnIndex(Inbox_Information_Table.INFO_ID));
					String title = cursor.getString(cursor.getColumnIndex(Inbox_Information_Table.TITLE));
					txtInboxInformationTitle.setText(title);
				}
			}
			break;
		}

	}

}